package com.yanqu.road.ssl;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

@Configuration
@ConfigurationProperties(prefix = "customer.server")
public class TomcatConfig {
    private static Logger logger = LogManager.getLogger(TomcatConfig.class.getName());
    private Boolean secure;
    private String scheme;
    private int port;
    private String SSLEnabled;
    private String keystoreFile;
    private String keystorePass;
    private boolean open;


    /**
     * 自定义tomcat配置
     */
    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                protocol.setMaxConnections(600);
            }
        });
        if (open) {
            try {
                tomcat.addAdditionalTomcatConnectors(createSslConnector());
            } catch (Exception e) {
                logger.error("异常错误",e);
            }
        }
        return tomcat;
    }

    /**
     * 创建SSL连接器
     */
    private Connector createSslConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setSecure(secure);
        connector.setScheme(scheme);
        connector.setPort(port);
        connector.setProperty("SSLEnabled", SSLEnabled);
        File file = new File(keystoreFile);
        if (file.exists()) {
            connector.setProperty("keystoreFile", file.getAbsolutePath());
        }else {
            throw new RuntimeException("证书文件不存在, " + file.getAbsolutePath());
        }
        connector.setProperty("keystorePass", keystorePass);
        return connector;
    }

    public void setSecure(Boolean secure) {
        this.secure = secure;
    }

    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setSSLEnabled(String SSLEnabled) {
        this.SSLEnabled = SSLEnabled;
    }

    public void setKeystoreFile(String keystoreFile) {
        this.keystoreFile = keystoreFile;
    }

    public void setKeystorePass(String keystorePass) {
        this.keystorePass = keystorePass;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }
}
